/**
 * Copyright 2010 Jake Jackson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jake.nb.ext.ant.hints.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import org.jake.nb.ext.ant.hints.AntHint;
import org.jake.nb.ext.ant.hints.Configuration;
import org.jake.nb.ext.ant.hints.Paths;
import org.jake.nb.ext.ant.hints.SpanContainer;
import org.jake.nb.ext.ant.parser.Span;
import org.jake.nb.ext.ant.parser.Spans;
import org.jake.nb.ext.ant.parser.TargetSpan;
import org.netbeans.modules.editor.NbEditorUtilities;
import org.netbeans.spi.editor.hints.ErrorDescription;
import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
import org.netbeans.spi.editor.hints.Severity;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;

/**
 * An ant hint checking each target name is defined once only in the build file
 * @author JakeJ
 */
@ServiceProvider(service = AntHint.class, path = "org-jake-nb-ext-ant-hints")
public class DuplicateTargetName implements AntHint {

  private Configuration configuration;

  public DuplicateTargetName() {
    configuration = new Configuration(DuplicateTargetName.class.getCanonicalName(),
            "Duplicate target name",
            "Checks whether a target's name is used more than one time in a build script.",
            true,
            Severity.ERROR, Paths.TARGET);
  }



  @Override
  public Collection<ErrorDescription> getErrors(final SpanContainer container) {
    final Collection<ErrorDescription> errors = new HashSet<ErrorDescription>();
    final Map<String, Collection<Span>> duplicates = new HashMap<String, Collection<Span>>();
    final Spans targetSpans = container.getTargetSpans();
    for (final Span object : targetSpans.getSpans()) {
      final TargetSpan span = (TargetSpan) object;
      if (span.getNameSpan() != null && span.getName().trim().length() > 0) {
        Collection<Span> get = duplicates.get(span.getName());
        if (get == null) {
          get = new HashSet<Span>();
          duplicates.put(span.getName(), get);
        }
        get.add(span);
      }
    }

    for (final Collection<Span> collection : duplicates.values()) {
      if (collection.size() > 1) {
        for (Span span : collection) {
          errors.add(ErrorDescriptionFactory.createErrorDescription(configuration.getSeverity(),
                  NbBundle.getMessage(this.getClass(), "Desc_Err_DuplicateTarget", ((TargetSpan) span).getName(), collection.size()), // NOI18N
                  container.getDocument(), NbEditorUtilities.getLine(container.getDocument(), span.getStart(), true).getLineNumber() + 1));

        }
      }
    }

    return errors;
  }

  @Override
  public Configuration getConfiguration() {
    return configuration;
  }
}
